#! /bin/bash

#.sh file dir
workDir=$(cd $(dirname $0); pwd);
# cpu类型
cpuType=x86_64;
# 操作系统类型
osType=centos;
# 安装路径
setupDir=/opt/kingfusion3.6;
# docker安装文件路径
dockerDir=$setupDir/dockersetup/x86_64/centos;
# docker端口
dockerPort=2375;
# 基础镜像路径
baseImageDir=$setupDir/dockerimagedb/base/x86_64/linux;
# 本节点内网地址
opsProxyAddr=127.0.0.1;
# 本节点外网地址
proxyPublicAddr=127.0.0.1;
# 运维中心内网地址
opscenterAddr=127.0.0.1;
# 运维中心外网地址
opsPublicAddr=127.0.0.1;
# 节点名称
nodeName=node;
# 用户名
userName=admin001;
# 密码
password=123456;

# echo color
SETCOLOR_SUCCESS="echo -en \\033[1;32m";
SETCOLOR_FAILURE="echo -en \\033[1;31m";
SETCOLOR_TIP="echo -en \\033[1;34m";
SETCOLOR_NORMAL="echo -en \\033[0;39m";

# echo function
function echoSuccess(){
	$SETCOLOR_SUCCESS;
	echo $1 $2 $3 $4 $5 $6;
	$SETCOLOR_NORMAL;
}
function echoFailure(){
	$SETCOLOR_FAILURE;
	echo $1 $2 $3 $4 $5 $6;
	$SETCOLOR_NORMAL;
}
function echoTip(){
	$SETCOLOR_TIP;
	echo $1 $2 $3 $4 $5 $6;
	$SETCOLOR_NORMAL;
}
#ip校验
function isVaildIP(){
	local ip=$IP;
	local ret=1;
	if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
		ip=(${ip//\./ }) #按.分割
		[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
		ret=$?
	fi
	return $ret;	
}
#非法字符检验
function isVaild(){
	local ret=1;
	local strTemp=$node;
	#arrays=("\!" "\#")
	arrays=("\!" "\#" "\%" "\&" "\*" "\+" "\," "\/" "\-" "\." "\:" "\;" "\=" "\?" "\@" "\[" "\]" "\^" "\{" "\}" "\~");
	for item in ${arrays[@]}
	do
		result=$(echo "$strTemp" | grep "$item" );
		if [[ "$result" != "" ]];then
			ret=$?
			return $ret;
		fi
	done
	return $ret;
}
#非法字符检验
function isLength(){
	local ret=1;
	local strTemp=$node;
	local strLength=${#strTemp} ;
	if [ $strLength -gt 30 ]
	then
		ret=$?
	fi
	return $ret;
}
#是否以数字开头 
function isHeadFigure(){
	local strTemp=$node;
	local ret=1;
	arrays=("0" "1" "2" "3" "4" "5" "6" "7" "8" "9");
	for item in ${arrays[@]}
	do
		result=$(echo "${strTemp:0:1}" | grep "$item" );
		if [[ "$result" != "" ]];then
			ret=$?
			return $ret;
		fi
	done
	return $ret;
}
# 端口检测函数
function checkPort(){
	while true
	do
		bUser=false;
		userPort="";
		array=(443 11006 11007 12000 12001 12002 8803 3030);
		for item in ${array[@]}
		do
			check=$(netstat -tunlp | grep ":$item " | wc -l);
			if [ $check -ne 0 ]
			then
				bUser=true;
				userPort="$userPort $item";
			fi
		done
	
		if [ "$bUser" == "true" ]
		then
			echoFailure "$userPort 端口被占用，请关闭端口";
			echoTip "[N] 已关闭端口,继续安装\n";
			echoTip "[E] 退出安装程序\n";
			read -p "请输入您要进行的操作:" input;
			if [[ "$input" == "E" ]] || [[ "$input" == "e" ]]
			then
				# 退出安装程序
				echoTip "退出安装程序";
				exit;
			elif [[ "$input" == "N" ]] || [[ "$input" == "n" ]]
			then
				echoTip "已关闭端口,继续安装";
			else
				# 不识别的操作
				echoFailure "不识别的操作,安装程序退出";
				exit;
			fi
		else
			# 防火墙开端口
			check=$(sudo systemctl status firewalld.service | grep "running" | wc -l);
			if [ $check -ne 0 ]
			then
				arrays=(443 11006 11007 12000 12001 12002 8803 3030 13000);
				for item in ${arrays[@]}
				do
					sudo firewall-cmd --zone=public --add-port=$item/tcp --permanent;
				done
				sudo firewall-cmd --reload;
			fi
			# 退出循环
			break;
		fi
	done
}

# installDocker function
function installDocker(){
	# install docker
	check=$(yum list installed | grep docker-ce-cli | wc -l);
	if [ $check -ne 0 ]
	then
		echoTip "docker has installed";
	else
		echo "start install docker ……";

		# install container-selinux
		check=$(yum list installed | grep container-selinux | wc -l);
		if [ $check -ne 0 ]
		then
			echoTip "container-selinux have installed";
		else
			sudo rpm -ivh $dockerDir/container-selinux-2.107-3.el7.noarch.rpm;
			if test $? -eq 0
			then
				echoSuccess "container-selinux installe success";
			else
				echoFailure "container-selinux installe failed";
				exit 1;
			fi
		fi

		# install container-io
		check=$(yum list installed | grep containerd.io | wc -l);
		if [ $check -ne 0 ]
		then
			echoTip "containerd.io have installed";
		else
			sudo rpm -ivh $dockerDir/containerd.io-1.2.6-3.3.el7.x86_64.rpm;
			if test $? -eq 0
			then
				echoSuccess "containerd.io installe success";
			else
				echoFailure "containerd.io installe failed";
				exit 1;
			fi
		fi

		# install docker-ce-cli and docker-ce
		check=$(yum list installed | grep docker-ce | wc -l);
		if [ $check -ne 0 ]
		then
			echoTip "docker-ce have installed";
			# install docker-ce-cli
			sudo rpm -ivh $dockerDir/docker-ce-cli-19.03.2-3.el7.x86_64.rpm;
			if test $? -eq 0
			then
				echoSuccess "docker-ce-cli installe success";
			else
				echoFailure "docker-ce-cli installe failed";
				exit 1;
			fi
		else
			# install docker-ce-cli
			sudo rpm -ivh $dockerDir/docker-ce-cli-19.03.2-3.el7.x86_64.rpm;
			if test $? -eq 0
			then
				echoSuccess "docker-ce-cli installe success";
			else
				echoFailure "docker-ce-cli installe failed";
				exit 1;
			fi
			# install docker-ce
			sudo rpm -ivh $dockerDir/docker-ce-19.03.2-3.el7.x86_64.rpm;
			if test $? -eq 0
			then
				echoSuccess "docker-ce installe success";
			else
				echoFailure "docker-ce installe failed";
				exit 1;
			fi
		fi

		# start docker
		sudo systemctl start docker;
		if test $? -eq 0
		then
			echoSuccess "start docker success";
		else
			echoFailure "start docker failed";
			exit 1;
		fi
		# autorunning add into docker deamon
		sudo systemctl enable docker;
		if test $? -eq 0
		then
			echoSuccess "enable docker success";
		else
			echoFailure "enable docker failed";
			exit 1;
		fi
		# restart docker
		sudo systemctl restart docker;
		if test $? -eq 0
		then
			echoSuccess "restart docker success";
		else
			echoFailure "restart docker failed";
			exit 1;
		fi
		
		# set docker log max size
		if [ ! -d "/etc/docker/" ]
		then 
			mkdir /etc/docker;
		fi
		
		limit={"\"log-driver\"":"\"json-file\"","\"log-opts\"":{"\"max-size\"":"\"20m\"","\"max-file\"":"\"1\""}};
		
		if [ ! -f "/etc/docker/daemon.json" ]
		then 
			echo $limit > /etc/docker/daemon.json;
		fi 
		# set docker.sock limit
		sudo chmod 777 /var/run/docker.sock;
		if test $? -ne 0
		then
			echoFailure "set docker.sock limit failed";
			exit 1;
		fi
		# set docker.service limit
		sudo chmod a+rw /lib/systemd/system/docker.service;
		if test $? -ne 0
		then
			echoFailure "set docker.service limit failed";
			exit 1;
		fi
		# config docker.service
		check=$(sudo cat /lib/systemd/system/docker.service | grep -n ExecStart= | awk -F ":" '{print $1}' | wc -l);
		if [ $check -ne 0 ]
		then
			echo "start docker API……";
			
			row=$(sudo cat /lib/systemd/system/docker.service | grep -n ExecStart= | awk -F ":" '{print $1}');
			sudo sed -i ""$row"c ExecStart=/usr/bin/dockerd -H  fd:// -H tcp://127.0.0.1:$dockerPort" /lib/systemd/system/docker.service;
			if test $? -eq 0
			then
				sudo systemctl daemon-reload;
				sudo systemctl restart docker;
				echoSuccess "the docker API start success";
			else
				echoFailure "new docker.service failed";
				exit 1;
			fi
		else
			echoFailure "can not find docker.service,start dockerAPI failed";
			exit 1;
		fi
	fi
}
#load kingfusionbase image function
function loadKFBaseImage(){
	check=$(sudo docker images kingfusionbase | grep kingfusionbase | wc -l);
	if [ $check -ne 0 ]
	then
		echoTip "kingfusionbase has loaded";
	else
		echo "start load  kingfusionbase ……";
		
		sudo docker load < $baseImageDir/kingfusionbase.tar;
		if test $? -eq 0
		then
			echoSuccess "load kingfusionbase image success";
		else
			echoFailure "load kingfusionbase image failed";
			exit 1;
		fi
	fi
}

function setupDevOpsCenter(){

	echoTip "开始安装开发/运维中心";

	# 拷贝安装卸载脚本
	#sudo cp $workDir/安装说明.txt $setupDir/setup;
	sudo cp $workDir/uninstall.sh $setupDir/setup;
	sudo cp $workDir/updatesetup.sh $setupDir/setup;
	# 解压开发运维中心安装包文件
	sudo unzip -O CP936 -o $workDir/package/kingdevopscenter.kfz "config/*" -d $setupDir;
	sudo unzip -O CP936 -o $workDir/package/kingdevopscenter.kfz "dockerimagedb/*" -d $setupDir;
	sudo unzip -O CP936 -o $workDir/package/kingdevopscenter.kfz "dockersetup/*" -d $setupDir;
	sudo unzip -O CP936 -o $workDir/package/kingdevopscenter.kfz "exedb/*" -d $setupDir;
	sudo unzip -O CP936 -o $workDir/package/kingdevopscenter.kfz "kingdevopscenter3.6/*" -d $setupDir;
	# 判断sdb文件夹是否存在,不存在就解压,存在的话就不解压(不覆盖工程文件)
	if [ ! -d "$setupDir/sdb/" ]
	then
		sudo unzip -O CP936 -o $workDir/package/kingdevopscenter.kfz "sdb/*" -d $setupDir;
	fi
	# 授权执行环境
	sudo chmod 777 $setupDir/exedb/kingcalculation/x86/linux/1.0.0.1/start.sh;
	sudo chmod 777 $setupDir/exedb/kingclient/x86/linux/1.0.0.1/start.sh;
	# sudo chmod 777 $setupDir/exedb/kingdb/x86/linux/1.0.0.1/start.sh;
	sudo chmod 777 $setupDir/exedb/kingdbconnector/x86/linux/1.0.0.1/start.sh;
	sudo chmod 777 $setupDir/exedb/kingioserver/x86/linux/1.0.0.1/start.sh;
	sudo chmod 777 $setupDir/exedb/kingalarmcal/x86/linux/1.0.0.1/start.sh;
	# 授权开发环境
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingcalculation/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingclient/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingdbmanger/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingguard/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingide/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingioserver/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingoauth/exe/start.sh;
	sudo chmod -R 777 $setupDir/kingdevopscenter3.6/exe/kingcalculation/exe/solutions;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingfileserver/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingopsserver/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingservicediscovery/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingtileserver/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kingpermissionservice/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kinglogservice/exe/start.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/exe/kinglicense/exe/serviceMonitor.sh;
	sudo chmod 777 $setupDir/kingdevopscenter3.6/data/elasticsearch/start.sh;
	# 授权安装目录的脚本
	sudo chmod 777 $setupDir/setup/*
	# 开防火墙
	# open firewalld.service port
	check=$(sudo systemctl status firewalld.service | grep "running" | wc -l);
	if [ $check -ne 0 ]
	then
		array=(443 11006 11007 12000 12001 12002 8803 3030);
		for item in ${array[@]}
		do
			sudo firewall-cmd --zone=public --add-port=$item/tcp --permanent;
		done
		sudo firewall-cmd --reload;
	fi
	
	#授权服务看门狗自启动
	sudo chmod +x /etc/rc.d/rc.local;
	echo "$setupDir/kingdevopscenter3.6/exe/kinglicense/exe/serviceMonitor.sh" >> /etc/rc.d/rc.local;
	cd $setupDir/kingdevopscenter3.6/exe/kinglicense/exe;
	sleep 1;
	./serviceMonitor.sh;

	#nginx配置文件设置授权服务代理地址
	checkLicenseAddr=$(sudo cat $setupDir/kingdevopscenter3.6/data/nginx/config/nginx.conf | grep -n "location \/kinglicense\/" | awk -F ":" '{print $1}' | wc -l);
	if [ $checkLicenseAddr -ne 0 ]
	then
		echo "配置nginx.conf::kinglicense";
		row=$(sudo cat $setupDir/kingdevopscenter3.6/data/nginx/config/nginx.conf | grep -n "location \/kinglicense\/" | awk -F ":" '{print $1}' | sed -n '1p');
		rowa=$(($row+1));
		sudo sed -i ""$rowa"c \	\	\	proxy_pass http:\/\/$devOpsAddr:11007\/;" $setupDir/kingdevopscenter3.6/data/nginx/config/nginx.conf;
	else
		echo "配置失败，nginx.conf::kinglicense未找到";
		exit 1;
	fi

	# 安装docker
	installDocker;
	# 加载基础镜像
	loadKFBaseImage;
	# 创建容器,启动看门狗
	check=$(sudo docker inspect kingdevopscenter | grep kingdevopscenter | wc -l);
	if [ $check -eq 0 ]
	then
		echo "创建开发/运维中心容器 ……";
		
		sudo docker run -i -t -d -p 443:443 -p 6379:6379 -p 12000:12000 -p 12001:12001 -p 12002:12002 -p 11006:11006 -p 8803:8803 -p 3030:3030 --restart=always --name kingdevopscenter  -v $setupDir/:/kingfusion/ -v $setupDir/kingdevopscenter3.6/data/nginx/cert:/var/cert -v $setupDir/kingdevopscenter3.6/data/nginx/config/nginx.conf:/usr/local/nginx/conf/nginx.conf -v $setupDir/kingdevopscenter3.6/data/nginx/log:/usr/local/nginx/logs -v $setupDir/kingdevopscenter3.6/data/redis/data:/etc/redis/data -v $setupDir/kingdevopscenter3.6/data/redis/config:/etc/redis/config -v $setupDir/kingdevopscenter3.6/data/redis/log:/etc/redis/log -e HOST=$devOpsAddr kingfusionbase sh -c "cd /kingfusion/kingdevopscenter3.6/exe/kingguard/exe && ./start.sh";
			
		if test $? -eq 0
		then
			#echoSuccess "创建开发中心/运维容器成功";
			echoSuccess "开发中心/运维中心安装成功";
		else
			#echoFailure "创建开发中心/运维容器失败";
			echoSuccess "开发中心/运维中心安装失败";
		fi
	fi
	
	sh -c "cd $setupDir/kingdevopscenter3.6/data/elasticsearch && ./start.sh";
}

function setDevProxyConfig(){
	# 输入本节点内网地址
	read -p "请输入本节点内网地址,没有请输入 [N]:" addr;
	if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
	then
		opsProxyAddr=$addr;
	else
		IP=$addr;
		# if [ -n "$addr" ];then
			while ( ! isVaildIP $IP)
			do
				read -p "本节点内网地址:$IP not is vaild,请重新输入，没有请输入 [N]:" addr;
				IP=$addr;
			done
			opsProxyAddr=$addr;
			echoTip "本节点内网地址是 $opsProxyAddr";
		# fi
	fi
	# 输入本节点运维代理内网地址
	# check=$(ip addr |grep inet |grep -v inet6 |grep eth0|awk '{print $2}' |awk -F "/" '{print $1}' |wc -l);
	# check1=$(ip addr |grep inet |grep -v inet6 |grep eth1|awk '{print $2}' |awk -F "/" '{print $1}' |wc -l);
	# check2=$(ip addr |grep inet |grep -v inet6 |grep ens33|awk '{print $2}' |awk -F "/" '{print $1}' |wc -l);
	# check3=$(ip addr |grep inet |grep -v inet6 |grep eno1|awk '{print $2}' |awk -F "/" '{print $1}' |wc -l);
	# if [ $check -ne 0 ]
	# then
	# 	opsProxyAddr=$(ip addr |grep inet |grep -v inet6 |grep eth0|awk '{print $2}' |awk -F "/" '{print $1}');
	# 	echoTip "本节点内网地址(eth0): $opsProxyAddr";
	# elif [ $check1 -ne 0 ]
	# then
	# 	opsProxyAddr=$(ip addr |grep inet |grep -v inet6 |grep eth1|awk '{print $2}' |awk -F "/" '{print $1}');
	# 	echoTip "本节点内网地址(eth0): $opsProxyAddr";
	# elif [ $check2 -ne 0 ]
	# then
	# 	opsProxyAddr=$(ip addr |grep inet |grep -v inet6 |grep ens33|awk '{print $2}' |awk -F "/" '{print $1}');
	# 	echoTip "本节点内网地址(eth0): $opsProxyAddr";
	# elif [ $check3 -ne 0 ]
	# then
	# 	opsProxyAddr=$(ip addr |grep inet |grep -v inet6 |grep eno1|awk '{print $2}' |awk -F "/" '{print $1}');
	# 	echoTip "本节点内网地址(eth0): $opsProxyAddr";
	# else
	# 	read -p "未检测到本节点内网地址,请输入本节点内网地址:" addr;
	# 	opsProxyAddr=$addr;
	# 	echoTip "本节点内网地址: $opsProxyAddr";
	# fi
	# 输入本节点运维代理外网地址
	read -p "请输入本节点外网地址,没有请输入 [N]:" addr;
	if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
	then
		proxyPublicAddr=$opsProxyAddr;
	else
		IP=$addr;
		# if [ -n "$addr" ];then
			while ( ! isVaildIP $IP)
			do
				read -p "本节点外网地址:$IP not is vaild,请重新输入，没有请输入 [N]:" addr;
				IP=$addr;
				if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
				then
					break
				fi
			done
			proxyPublicAddr=$addr;
			echoTip "本节点外网地址是 $proxyPublicAddr";
		# fi
	fi
	# 输入运维中心内网地址
	read -p "请输入运维中心内网地址,没有请输入 [N]:" addr;
	if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
	then
		# 没有内网IP,说明本节点和运维中心不在一个局域网
		
		# 输入运维中心外网地址
		read -p "请输入运维中心外网地址,没有请输入 [N]::" addr;
		if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
		then 
			opsPublicAddr=$opsPublicAddr
		else
			IP=$addr;
			while ( ! isVaildIP $IP)
			do
				read -p "运维中心外网地址:$IP not is vaild,请重新输入,没有请输入 [N]:" addr;
				IP=$addr;
				if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
				then
					break
				fi
			done
			if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
			then
				opsPublicAddr=$opsPublicAddr;
				opscenterAddr=$opscenterAddr;	
			else
				echoTip "运维中心外网地址是 "$addr
				opsPublicAddr=$addr;
				opscenterAddr=$opsPublicAddr;		
			fi
		fi
	else
		IP=$addr;
		while ( ! isVaildIP $IP)
		do
			read -p "运维中心内网地址:$IP not is vaild,请重新输入,没有请输入 [N]:" addr;
			IP=$addr;
			if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
			then
				break
			fi
		done
		if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
		then
			opscenterAddr=$opscenterAddr;
		else
			# 有内网IP,说明本节点和运维中心在一个局域网
			echoTip "运维中心内网地址是 "$addr
			opscenterAddr=$addr;
		fi
		
		# 输入运维中心外网地址
		read -p "请输入运维中心外网地址,没有请输入 [N]:" addr;
		if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
		then
			# 没有外网IP,前端只能局域网内访问
			opsPublicAddr=$opscenterAddr;
		else
			# 有内网IP,有外网IP,前端走外网,后台走内网
			IP=$addr;
			while ( ! isVaildIP $IP)
			do
				read -p "运维中心外网地址:$IP not is vaild,请重新输入,没有请输入 [N]:" addr;
				IP=$addr;
				if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
				then
					break
				fi
			done
			if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
			then
				opsPublicAddr=$opsPublicAddr;
			else
				echoTip "运维中心外网地址是 "$addr
				opsPublicAddr=$addr;
			fi			
		fi
	fi
		
	# 输入节点名称
	read -p "请输入节点名称:" node;
	# str=$node;
	while ( [ "$node" == "" ] || isVaild $node || isHeadFigure $node || isLength $node )
	do
		read -p "节点名称为空、或者不能以数字开头、有非法字符、字符超过32个，请重新输入:" node;
	done
	# if [ "$node" = "" ];then
	# 	echo "节点名称:$str is null,安装程序退出!"
	# 	exit;
	# fi
	# while ( isVaild $node )
	# do

	# done
	# if isVaild $str;then
	# 	echo "节点名称:$str not is vaild,安装程序退出!"
	# 	exit;
	# fi
	# if isHeadFigure $str;then
	# 	echo "节点名称:$str 不能以数字开头,安装程序退出!"
	# 	exit;
	# fi
	nodeName=$node;
	# 输入用户名
	read -p "请输入用户名:" name;
	node=$name;
	while ( [ "$node" == "" ] || isVaild $node || isHeadFigure $node || isLength $node)
	do
		read -p "用户名为空、或者不能以数字开头、有非法字符、字符超过32个，请重新输入:" name;
		node=$name;
	done
	# str=$name
	# if [ "$name" = "" ];then
	# 	echo "用户名:$str is null,安装程序退出!"
	# 	exit;
	# fi
	# if isVaild $str;then
	# 	echo "用户名:$str not is vaild,安装程序退出!"
	# 	exit;
	# fi
	userName=$name;
	# 输入密码
	stty -echo
	read -p $'请输入密码: \n' pwd;
	while ( [ "$pwd" == "" ] )
	do
		read -p $'密码不能为空，请重新输入: \n'	pwd;
	done
	password=$pwd;
	stty echo
		
	echoSuccess "本节点内网地址是: $opsProxyAddr";
	echoSuccess "本节点外网地址是: $proxyPublicAddr";
	echoSuccess "运维中心内网地址是: $opscenterAddr";
	echoSuccess "运维中心外网地址是: $opsPublicAddr";
	echoSuccess "节点名称是: $nodeName";
	echoSuccess "用户名是: $userName";
}

function setupDevProxy(){

	echoTip "开始安装运维代理";

	# 拷贝安装卸载软件
	#sudo cp $workDir/安装说明.txt $setupDir/setup;
	sudo cp $workDir/uninstall.sh $setupDir/setup;
	sudo cp $workDir/updatesetup.sh $setupDir/setup;
	sudo cp $workDir/changeIP.sh $setupDir/setup;
	sudo cp $workDir/changeUser.sh $setupDir/setup;
	# 解压开发运维中心安装包文件
	sudo unzip -O CP936 -o $workDir/package/kingopsproxy.kfz "config/*" -d $setupDir;
	sudo unzip -O CP936 -o $workDir/package/kingopsproxy.kfz "dockersetup/*" -d $setupDir;
	sudo unzip -O CP936 -o $workDir/package/kingopsproxy.kfz "kingopsproxy3.6/*" -d $setupDir;
	# 授权运行环境
	sudo chmod -R 777 $setupDir/exedb;
	sudo chmod -R 777 $setupDir/kingopsproxy3.6/exe/kingopsagent/exe/data;
	sudo chmod 777 $setupDir/kingopsproxy3.6/exe/kingguard/exe/start.sh;
	sudo chmod 777 $setupDir/kingopsproxy3.6/exe/kingopsagent/exe/start.sh;
	sudo chmod 777 $setupDir/kingopsproxy3.6/exe/kingfilebeat/start.sh;
	sudo chmod 777 $setupDir/kingopsproxy3.6/exe/kingmetricbeat/start.sh;
	# 授权安装目录的脚本
	sudo chmod 777 $setupDir/setup/*;
	# 开防火墙
	# open firewalld.service port
	check=$(sudo systemctl status firewalld.service | grep "running" | wc -l);
	if [ $check -ne 0 ]
	then
		array=(13000);
		for item in ${array[@]}
		do
			sudo firewall-cmd --zone=public --add-port=$item/tcp --permanent;
		done
		sudo firewall-cmd --reload;
	fi
	
	check=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"docker\":" | awk -F ":" '{print $1}' | wc -l);
	if [ $check -ne 0 ]
	then
		echoTip "读取运维代理文件,proxyconfig.json:docker";
		
		row=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"docker\":" | awk -F ":" '{print $1}' | sed -n '1p');
		row=$(($row+0));
		rowa=$(($row+2));
		check=$(sudo sed -n ""$rowa"p" $setupDir/config/proxyconfig.json | grep -n "\"active\": true" | wc -l);
		check1=$(sudo sed -n ""$rowa"p" $setupDir/config/proxyconfig.json | grep -n "\"active\":true" | wc -l);
		if [[ $check -ne 0 ]] || [[ $check1 -ne 0 ]]
		then
			echoTip "docker运维APP";
			opsType=docker;
		else
			echoTip "非docker运维APP";
			opsType=noDocker;
		fi
	else
		echoFailure "读取运维代理文件失败,proxyconfig.json:docker未找到";
		exit 1;
	fi
	# docker运维,安装docker
	if [ "$opsType" == "docker" ]
	then
		# 安装docker
		installDocker;
	fi
	# 设置配置文件,运维中心内网地址
	check=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"opsCenterAddress\":" | awk -F ":" '{print $1}' | wc -l);
	if [ $check -ne 0 ]
	then
		echo "配置运维代理文件,proxyconfig.json:opsCenterAddress";
		row=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"opsCenterAddress\":" | awk -F ":" '{print $1}' | sed -n '1p');
		row=$(($row+0));
		rowa=$(($row+1));
		check=$(sudo sed -n ""$rowa"p" $setupDir/config/proxyconfig.json | grep -n "}" | wc -l);
		if [ $check -ne 0 ]
		then
			sudo sed -i ""$row"c \"opsCenterAddress\":\"$opscenterAddr\"" $setupDir/config/proxyconfig.json;
		else
			sudo sed -i ""$row"c \"opsCenterAddress\":\"$opscenterAddr\"," $setupDir/config/proxyconfig.json;
		fi
	else
		echoFailure "配置运维代理文件失败,proxyconfig.json:opsCenterAddress 未找到";
		exit 1;
	fi
	# 设置配置文件,运维中心外网地址
	check=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"opsPublicAddress\":" | awk -F ":" '{print $1}' | wc -l);
	if [ $check -ne 0 ]
	then
		echo "配置运维代理文件,proxyconfig.json:opsPublicAddress";
		row=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"opsPublicAddress\":" | awk -F ":" '{print $1}' | sed -n '1p');
		row=$(($row+0));
		rowa=$(($row+1));
		check=$(sudo sed -n ""$rowa"p" $setupDir/config/proxyconfig.json | grep -n "}" | wc -l);
		if [ $check -ne 0 ]
		then
			sudo sed -i ""$row"c \"opsPublicAddress\":\"$opsPublicAddr\"" $setupDir/config/proxyconfig.json;
		else
			sudo sed -i ""$row"c \"opsPublicAddress\":\"$opsPublicAddr\"," $setupDir/config/proxyconfig.json;
		fi
	else
		echoFailure "配置运维代理文件失败,proxyconfig.json:opsPublicAddress 未找到";
		exit 1;
	fi
	
	# 设置配置文件,本节点内网地址
	check=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"opsProxy\":" | awk -F ":" '{print $1}' | wc -l);
	if [ $check -ne 0 ]
	then
		echo "配置运维代理文件,proxyconfig.json:opsProxy";
		row=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"opsProxy\":" | awk -F ":" '{print $1}' | sed -n '1p');
		row=$(($row+0));
		rowa=$(($row+1));
		check=$(sudo sed -n ""$rowa"p" $setupDir/config/proxyconfig.json | grep -n "}" | wc -l);
		if [ $check -ne 0 ]
		then
			sudo sed -i ""$row"c \"opsProxy\":\"$opsProxyAddr\"" $setupDir/config/proxyconfig.json;
		else
			sudo sed -i ""$row"c \"opsProxy\":\"$opsProxyAddr\"," $setupDir/config/proxyconfig.json;
		fi
	else
		echoFailure "配置运维代理文件失败,proxyconfig.json:opsProxy 未找到";
		exit 1;
	fi
	# 设置配置文件,本节点外网地址
	check=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"proxyPublicAddress\":" | awk -F ":" '{print $1}' | wc -l);
	if [ $check -ne 0 ]
	then
		echo "配置运维代理文件,proxyconfig.json:proxyPublicAddress";
		row=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"proxyPublicAddress\":" | awk -F ":" '{print $1}' | sed -n '1p');
		row=$(($row+0));
		rowa=$(($row+1));
		check=$(sudo sed -n ""$rowa"p" $setupDir/config/proxyconfig.json | grep -n "}" | wc -l);
		if [ $check -ne 0 ]
		then
			sudo sed -i ""$row"c \"proxyPublicAddress\":\"$proxyPublicAddr\"" $setupDir/config/proxyconfig.json;
		else
			sudo sed -i ""$row"c \"proxyPublicAddress\":\"$proxyPublicAddr\"," $setupDir/config/proxyconfig.json;
		fi
	else
		echoFailure "配置运维代理文件失败,proxyconfig.json:proxyPublicAddress 未找到";
		exit 1;
	fi
	# 设置配置文件,oauth地址
	check=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"oauth\":" | awk -F ":" '{print $1}' | wc -l);
	if [ $check -ne 0 ]
	then
		echoTip "配置运维代理文件,proxyconfig.json:oauth";
		
		row=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"oauth\":" | awk -F ":" '{print $1}' | sed -n '1p');
		row=$(($row+0));
		rowactive=$(sudo sed -n ""$row",\$p" $setupDir/config/proxyconfig.json | grep -n "\"active\":" | awk -F ":" '{print $1}' | sed -n '1p');
		rowactive=$(($rowactive+0));
		if [ $rowactive -ne 0 ]
		then
			rowactive=$(($row+$rowactive-1));
			rowaddr=$(sudo sed -n ""$rowactive",\$p" $setupDir/config/proxyconfig.json | grep -n "\"address\":" | awk -F ":" '{print $1}' | sed -n '1p');
			rowaddr=$(($rowaddr+0));
			if [ $rowaddr -ne 0 ]
			then
				rowaddr=$(($rowactive+$rowaddr-1));
				rowa=$(($rowaddr+1));
				check=$(sudo sed -n ""$rowa"p" $setupDir/config/proxyconfig.json | grep -n "}" | wc -l);
				if [ $check -ne 0 ]
				then
					sudo sed -i ""$rowaddr"c \"address\":\"$opscenterAddr\"" $setupDir/config/proxyconfig.json;
				else
					sudo sed -i ""$rowaddr"c \"address\":\"$opscenterAddr\"," $setupDir/config/proxyconfig.json;
				fi
			else
				echoFailure "配置运维代理文件失败,proxyconfig.json:oauth::active::address未找到";
				exit 1;
			fi
		else
			echoFailure "配置运维代理文件失败,proxyconfig.json:oauth::active未找到";
			exit 1;
		fi
	else
		echoFailure "配置运维代理文件失败,proxyconfig.json:oauth未找到";
		exit 1;
	fi

	# 设置配置文件,redis地址
	check=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"redis\":" | awk -F ":" '{print $1}' | wc -l);
	if [ $check -ne 0 ]
	then
		echoTip "配置运维代理文件,proxyconfig.json:redis";
		row=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"redis\":" | awk -F ":" '{print $1}' | sed -n '1p');
		row=$(($row+0));
		rowhost=$(sudo sed -n ""$row",\$p" $setupDir/config/proxyconfig.json | grep -n "\"host\":" | awk -F ":" '{print $1}' | sed -n '1p');
		rowhost=$(($rowhost+0));
		if [ $rowhost -ne 0 ]
		then
			rowhost=$(($row+$rowhost-1));
			rowa=$(($rowhost+1));
			check=$(sudo sed -n ""$rowa"p" $setupDir/config/proxyconfig.json | grep -n "}" | wc -l);
			if [ $check -ne 0 ]
			then
				sudo sed -i ""$rowhost"c \"host\":\"$opscenterAddr:6379\"" $setupDir/config/proxyconfig.json;
			else
				sudo sed -i ""$rowhost"c \"host\":\"$opscenterAddr:6379\"," $setupDir/config/proxyconfig.json;
			fi
		else
			echoFailure "配置运维代理文件失败,proxyconfig.json:redis::active未找到";
			exit 1;
		fi
	else
		echoFailure "配置运维代理文件失败,proxyconfig.json:redis未找到";
		exit 1;
	fi

	# 设置配置文件,节点名称
	check=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"nodeName\":" | awk -F ":" '{print $1}' | wc -l);
	if [ $check -ne 0 ]
	then
		echoTip "配置运维代理文件,proxyconfig.json:nodeName";
		
		row=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"nodeName\":" | awk -F ":" '{print $1}' | sed -n '1p');
		row=$(($row+0));
		rowa=$(($row+1));
		check=$(sudo sed -n ""$rowa"p" $setupDir/config/proxyconfig.json | grep -n "}" | wc -l);
			if [ $check -ne 0 ]
			then
				sudo sed -i ""$row"c \"nodeName\":\"$nodeName\"" $setupDir/config/proxyconfig.json;
			else
				sudo sed -i ""$row"c \"nodeName\":\"$nodeName\"," $setupDir/config/proxyconfig.json;
			fi
	else
		echoFailure "配置运维代理文件失败,proxyconfig.json:nodeName";
		exit 1;
	fi

	# 设置配置文件,用户名
	check=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"username\":" | awk -F ":" '{print $1}' | wc -l);
	if [ $check -ne 0 ]
	then
		echoTip "配置运维代理文件,proxyconfig.json:username";
		row=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"username\":" | awk -F ":" '{print $1}' | sed -n '1p');
		row=$(($row+0));
		rowa=$(($row+1));
		check=$(sudo sed -n ""$rowa"p" $setupDir/config/proxyconfig.json | grep -n "}" | wc -l);
			if [ $check -ne 0 ]
			then
				sudo sed -i ""$row"c \"username\":\"$userName\"" $setupDir/config/proxyconfig.json;
			else
				sudo sed -i ""$row"c \"username\":\"$userName\"," $setupDir/config/proxyconfig.json;
			fi
	else
		echoFailure "配置运维代理文件失败,proxyconfig.json:username";
		exit 1;
	fi
	# 设置配置文件,密码
	check=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"password\":" | awk -F ":" '{print $1}' | wc -l);
	if [ $check -ne 0 ]
	then
		echoTip "配置运维代理文件,proxyconfig.json:password";
		
		row=$(sudo cat $setupDir/config/proxyconfig.json | grep -n "\"password\":" | awk -F ":" '{print $1}' | sed -n '1p');
		row=$(($row+0));
		rowa=$(($row+1));
		check=$(sudo sed -n ""$rowa"p" $setupDir/config/proxyconfig.json | grep -n "}" | wc -l);
			if [ $check -ne 0 ]
			then
				sudo sed -i ""$row"c \"password\":\"$password\"" $setupDir/config/proxyconfig.json;
			else
				sudo sed -i ""$row"c \"password\":\"$password\"," $setupDir/config/proxyconfig.json;
			fi
	else
		echoFailure "配置运维代理文件失败,proxyconfig.json:password";
		exit 1;
	fi

	if [ "$opsType" == "docker" ]
	then
		# 安装docker
		installDocker;
		# 加载基础镜像
		# modified by lujiao.xiao for {bug11929:单独安装运维代理加载镜像失败} at 2021/5/31
		# loadKFBaseImage;
		# ended by lujiao.xiao at 2021/5/31
	fi	

	echoSuccess "运维代理安装成功";
	# 开启自启动和看门狗
	# 启动运维代理看门狗
	if [ -f "$setupDir/kingopsproxy3.6/exe/kingopsagent/exe/data/nodeRegister.json" ]
	then 
		rm -rf $setupDir/kingopsproxy3.6/exe/kingopsagent/exe/data/nodeRegister.json;
	fi
		
	if [ -f "$setupDir/kingopsproxy3.6/exe/kingopsagent/exe/data/OauthRegister.json" ]
	then 
		rm -rf $setupDir/kingopsproxy3.6/exe/kingopsagent/exe/data/OauthRegister.json;
	fi
	
	sleep 6;
	#看门狗自启动
	sudo chmod +x /etc/rc.d/rc.local;
	echo "$setupDir/kingopsproxy3.6/exe/kingguard/exe/start.sh" >> /etc/rc.d/rc.local;
	cd $setupDir/kingopsproxy3.6/exe/kingguard/exe;
	sleep 1;
	./start.sh;
}

function searchHost() {
	# 开发/运维中心内网地址
	#check=$(ip addr |grep inet |grep -v inet6 |grep eth0|awk '{print $2}' |awk -F "/" '{print $1}' |wc -l);
	#check1=$(ip addr |grep inet |grep -v inet6 |grep eth1|awk '{print $2}' |awk -F "/" '{print $1}' |wc -l);
	#check2=$(ip addr |grep inet |grep -v inet6 |grep ens33|awk '{print $2}' |awk -F "/" '{print $1}' |wc -l);
	#check3=$(ip addr |grep inet |grep -v inet6 |grep eno1|awk '{print $2}' |awk -F "/" '{print $1}' |wc -l);
	#if [ $check -ne 0 ]
	#then
	#	devOpsAddr=$(ip addr |grep inet |grep -v inet6 |grep eth0|awk '{print $2}' |awk -F "/" '{print $1}');
	#elif [ $check1 -ne 0 ]
	#then
	#	devOpsAddr=$(ip addr |grep inet |grep -v inet6 |grep eth1|awk '{print $2}' |awk -F "/" '{print $1}');
	#elif [ $check2 -ne 0 ]
	#then
	#	devOpsAddr=$(ip addr |grep inet |grep -v inet6 |grep ens33|awk '{print $2}' |awk -F "/" '{print $1}');
	#elif [ $check3 -ne 0 ]
	#then
	#	devOpsAddr=$(ip addr |grep inet |grep -v inet6 |grep eno1|awk '{print $2}' |awk -F "/" '{print $1}');
	#else
		read -p "请输入开发/运维中心内网地址:" addr;
		IP=$addr;
		while ( ! isVaildIP $IP )
		do
			read -p "开发/运维中心内网地址:$IP not is vaild,请重新输入,没有请输入 [N]:" addr;
			IP=$addr;
			if [[ "$addr" == "n" ]] || [[ "$addr" == "N" ]]
			then
				break
			fi
		done
		export devOpsAddr=$IP;
	#fi
}


# 安装目录
read -p "请输入安装目录,默认为: /opt/kingfusion3.6 ,安装在默认目录,请直接回车:" dir;
if [[ "$dir" != "" ]]
then
	result=$(echo $dir | grep "^/") ;
	if [ -n "$result" ]
	then
		arrays=("\!" "\"" "\#" "\%" "\&" "\*" "\+" "\," "\-"  "\:" "\;" "\=" "\?" "\@" "\[" "\]" "\^" "\{" "\}" "\~");
		for item in ${arrays[@]}
		do
			result=$(echo $dir | grep $item | wc -l);
			if [ $result -ne 0 ]
			then
				echoFailure "路径中不能包含特殊字符"
				exit;
			fi
		done

		if [ $dir == "/" ];
		then 
			mkdir -p "/KingFusion3.6";
			dir="/KingFusion3.6";
		fi

		if [ ! -d "$dir" ]
		then
			mkdir -p $dir;
		fi
		count=`ls $dir|wc -w`;

		if [  "$count">"0" ]
		then
			dir=$dir/KingFusion3.6
		else
			mkdir -p $dir/KingFusion3.6;
			dir=$dir/KingFusion3.6;
		fi
		
		mkdir -p $dir/setup;
		setupDir=$dir;
	else
		echoFailure "请从根路径开始输入"
		exit;
	fi
else
	mkdir -p $setupDir/setup;
fi

# 设置docker和基础镜像路径
if [ "$cpuType" == "x86_64" ]
then
	if [ "$osType" == "centos" ]
	then
		dockerDir=$setupDir/dockersetup/x86_64/centos;
	fi
	
	baseImageDir=$setupDir/dockerimagedb/base/x86_64/linux;
elif [ "$cpuType" == "arm64" ]
then
	if [ "$osType" == "centos" ]
	then
		dockerDir=$setupDir/dockersetup/arm64/centos;
	fi
	
	baseImageDir=$setupDir/dockerimagedb/base/arm64/linux;
fi

# 选择安装的软件
echo "[1] 安装开发/运维中心";
echo "[2] 安装运维代理";
echo "[E] 退出安装程序";
read -p "请输入您要进行的操作:" input;

if [[ "$input" == "E" ]] || [[ "$input" == "e" ]]
then
	# 退出安装程序
	echoTip "退出安装程序";
	exit;
fi

if [ "$input" == "1" ]
then
	# 端口检测调用函数
	checkPort;
	# 获取本机IP
	searchHost;
	# 安装开发中心,调用函数
	setupDevOpsCenter;
elif [ "$input" == "2" ]
then
	# 输入运维代理配置参数
	setDevProxyConfig;
	# 安装运维代理
	setupDevProxy;
elif [ "$input" == "12" ]
then
	# 端口检测调用函数
	checkPort;
	# 获取本机IP
	searchHost;
	# 输入运维代理配置参数
	setDevProxyConfig;
	# 安装开发中心,调用函数
	setupDevOpsCenter;
	# 安装运维代理
	setupDevProxy;
else
	# 不识别的操作
	echoFailure "不识别的操作,安装程序退出";
	exit;
fi

